﻿using AutoMapper;
using InterfaceService;
using Model.Entity;
using Model.Request;
using Model.Response;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class MenuService : IMenuService
    {
        private readonly IMapper _mapper;
        private ISqlSugarClient _db;

        public MenuService(ISqlSugarClient db, IMapper mapper)
        {
            _db = db;
            _mapper = mapper;
        }

        /// <summary>
        /// 新增菜单
        /// </summary>
        /// <param name="req"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<bool> Add(MenuAdd req, string userId)
        { 
            Menu info = _mapper.Map<Menu>(req);
            info.Id = Guid.NewGuid().ToString();
            info.CreateUserId = userId;
            info.CreateTime = DateTime.Now;
            info.IsEnable= true;
            return await _db.Insertable(info).ExecuteCommandAsync() > 0;
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="req"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<bool> Edit(MenuEdit req, string userId)
        {
            var info = await _db.Queryable<Menu>().FirstAsync(p => p.Id == userId);
            _mapper.Map(req, info);
            //ModifyUserId
            //ModifyDate
            return await _db.Updateable(info).ExecuteCommandAsync() > 0;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> Del(string id)
        {
            var info =await _db.Queryable<Menu>().FirstAsync(p => p.Id == id);
            return await _db.Deleteable(info).ExecuteCommandAsync() > 0;
        }

        /// <summary>
        /// 批量删除菜单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<bool> BatchDel(string ids)
        {
            var list = await _db.Queryable<Menu>().FirstAsync(p => ids.Contains(p.Id));
            return await _db.Deleteable<Menu>(list).ExecuteCommandAsync() > 0;
        }

        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="req"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<List<MenuRes>> GetMenus(MenuReq req, string userId)
        {
            //查询用户信息，判断是否是超级管理员
            var user = await _db.Queryable<Users>().FirstAsync(p => p.Id == userId);
            if (user.UserType == 0)
            {
                var list = await _db.Queryable<Menu>()
                    .WhereIF(!string.IsNullOrEmpty(req.Name), m => m.Name.Contains(req.Name))
                    .WhereIF(!string.IsNullOrEmpty(req.Index), m => m.Index.Contains(req.Index))
                    .WhereIF(!string.IsNullOrEmpty(req.FilePath), m => m.FilePath.Contains(req.FilePath))
                    .WhereIF(!string.IsNullOrEmpty(req.Decription), m => m.Decription.Contains(req.Decription))
                    .OrderBy(m => m.order)
                    .Select(m => new MenuRes() { }, true)
                    //.ToTreeAsync(it => it.Children, it => it.ParentId,""))
                    .ToListAsync();

                return list;
            }
            else 
            {
                return null;
            }
        }

        public async Task<bool> SettingMenu(string roleId, string MenuId)
        {
            return false;
        }
    }
}
